Use the business logic layer
Please follow these steps to extend the sample presentation layer to use the functionality of the business logic layer:
After that your current form should look like this:
The columns of the DataGridView and an employeeBindingSource object are created from the Employee DataSource.
As next
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using MyProject.BusinessLogic.BusinessObjects; namespace MyProject.WinUI { public partial class Form1 : Form { public Form1() { InitializeComponent(); PersistenceManager.ConnectionString = @"Insert your connection string"; // load all Employee objects this.employeeBindingSource.DataSource = Employee.GetAll(); } private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { try { Employee employee = (Employee)this.employeeBindingSource.Current; employee.Persist(); } catch (Exception exception) { MessageBox.Show(exception.Message); e.Cancel = true; } } private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { try { Employee employee = (Employee)this.employeeBindingSource.Current; employee.Delete(); } catch (Exception exception) { MessageBox.Show(exception.Message); e.Cancel = true; } } } } |
If the DataGridView fires the RowValidating event, the current Employee is persisted.
If the DataGridView fires the UserDeletingRow event, the current Employee is deleted.
Now start the application and try to create a new Employee. If you leave the row, the application tries to persist the Employee. Before that the data is validated automatically. If the data is invalid the persistence is cancelled.
Notice that the validation of the data runs in the business logic layer and not in the presentation layer. So if you decide to develop an ASP.NET application or a web service that provides the same functionality as the Windows UI, you can use the same business logic layer.